package com.air.ota.vo;



import com.air.common.util.CommonUtil;
import com.air.ota.util.ConfigProperties;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.PBOCUtil;


/**
 * 交易请求数据
 * @author Marco
 * @param ver hex length=1 0x01
 * @param deviceNum hex length=4 
 * @param deviceSequence hex length=4
 * @param transDate bcd  YYYYMMDDhhmmss length=7
 * @param sztSequence hex length=4 0x00000000
 * @param samId 压缩bcd length=8
 * @param randomEncrypt hex length=16
 * @param randomEncryptVeri hex length=4
 * @param transTypeCode hex length=1
 * @param subTransTypeCode hex length=1
 * @param cardPhyType hex length=1 0x02
 * @param tranAppendCode hex length=1
 * @param mac hex length=4
 */
public abstract class SZTBaseReqTransVO {
	
	protected PBOCUtil pbocUtil = new PBOCUtil();
	
	protected byte[] sessionRandom;
	
	protected byte ver;
	protected byte[] deviceNum;
	protected byte[] deviceSequence;
	protected String transDate;//设备交易发起时间(设备时间)
	protected byte[] sztSequence;
	/*
	 * 以下3个为会话密钥安全信息 28字节
	 */
	protected byte[] samId;
	protected byte[] randomEncrypt;
	protected byte[] randomEncryptVeri;
	
	protected byte transTypeCode;
	protected byte subTransTypeCode;
	protected byte cardPhyType;
	protected byte tranAppendCode;
	
	protected byte[] transData;
	
	protected byte[] mac;
	
	
	
	public SZTBaseReqTransVO(byte[] deviceSequence,byte[] sztSequence,String deviceTriggerTime) throws Exception{
		
		this.transDate = deviceTriggerTime;
		
		this.sessionRandom = (CommonUtil.randomString(16)).toUpperCase().getBytes();
		//this.sessionRandom = HexStringUtil.hexStringToBytes("96D9346A42402D1741564B7F220D6929");
		
		
		byte[] sessionKey = HexStringUtil.hexStringToBytes(ConfigProperties.getInstance().getValue("szt.sessionKey"));
		byte[] veriCode={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
		
		
		this.deviceSequence = deviceSequence;
		this.sztSequence = sztSequence;
		
		
		this.ver = 0x01;
		this.deviceNum = HexStringUtil.hexStringToBytes(ConfigProperties.getInstance().getValue("szt.deviceNum"));
		
		this.cardPhyType = 0x03;
		
		this.samId = HexStringUtil.hexStringToBytes(ConfigProperties.getInstance().getValue("stz.samId"));
		
		this.randomEncrypt = pbocUtil.TripleDes(sessionKey, sessionRandom, 1);
		
		
		randomEncryptVeri = new byte[4];
		System.arraycopy(pbocUtil.TripleDes(sessionRandom, veriCode, 1), 0, randomEncryptVeri, 0, 4);
		
		
	}
	
	public String getTransDataHexString() throws Exception{
		StringBuffer hexStringBuf = new StringBuffer();
		
		hexStringBuf.append(HexStringUtil.byteToHexString(this.ver));
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.deviceNum));
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.deviceSequence));
		hexStringBuf.append(transDate);
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.sztSequence));
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.samId));
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.randomEncrypt));
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.randomEncryptVeri));
		hexStringBuf.append(HexStringUtil.byteToHexString(this.transTypeCode));
		hexStringBuf.append(HexStringUtil.byteToHexString(this.subTransTypeCode));
		hexStringBuf.append(HexStringUtil.byteToHexString(this.cardPhyType));
		hexStringBuf.append(HexStringUtil.byteToHexString(this.tranAppendCode));
		
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.transData));
		
		
		
		PBOCUtil desTest=  new PBOCUtil();
		//byte[] toMacData  = HexStringUtil.hexStringToBytes(hexStringBuf.toString());
		byte[] toMacData  = this.transData;
		byte[] intv = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
		this.mac = desTest.Mac(sessionRandom, toMacData, intv);
		
		hexStringBuf.append(HexStringUtil.bytesToHexString(this.mac));
		
		
		/*System.out.println("sessionRandom="+HexStringUtil.bytesToHexString(sessionRandom));
		System.out.println("transData="+HexStringUtil.bytesToHexString(toMacData));
		System.out.println("mac="+HexStringUtil.bytesToHexString(mac));*/
		
		System.out.println("SZT Online Request Data is: "+hexStringBuf.toString());
		System.out.println("SZT Online Trans Detail Request Data is: "+HexStringUtil.bytesToHexString(this.transData));
		
		return hexStringBuf.toString();
	}
	
}
